package com.gxuwz.medical.domain.chronicdis;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.UserNotFoundException;

/**
 * 慢性病分类管理类
 * 
 * @author 演示
 * 
 */
public class Chronicdis {

	private String chroniccode;
	private String pinyincode;
	private String chronicname;

	public String getChroniccode() {
		return chroniccode;
	}

	public void setChroniccode(String chroniccode) {
		this.chroniccode = chroniccode;
	}

	public String getPinyincode() {
		return pinyincode;
	}

	public void setPinyincode(String pinyincode) {
		this.pinyincode = pinyincode;
	}

	public String getChronicname() {
		return chronicname;
	}

	public void setChronicname(String chronicname) {
		this.chronicname = chronicname;
	}

	public Chronicdis() {
	}
	public Chronicdis(String chroniccode)throws UserNotFoundException{
		load(chroniccode);
	}

	public Chronicdis(String chroniccode, String pinyincode, String chronicname) {
		this.chroniccode = chroniccode;
		this.pinyincode = pinyincode;
		this.chronicname = chronicname;
	}

	public void addChronic(String chroniccode, String pinyincode,
			String chronicname) throws SQLException {
		Connection conn = null;
		try {
			this.chroniccode = chroniccode;
			this.pinyincode = pinyincode;
			this.chronicname = chronicname;

			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 1:保存用户信息
			saveToDB(conn);
			// 提交事务
			conn.commit();

		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	protected void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_chronic(chroniccode,pinyincode,chronicname)");
			sqlBuff.append("values(?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = conn.prepareStatement(sql);
			// 依次设置动态参数的值，注意顺序
			int index = 1;
			pstmt.setString(index++, this.chroniccode);// 行政区域编码
			pstmt.setString(index++, this.pinyincode);
			pstmt.setString(index++, this.chronicname);
			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}
	}
	
	public void delChronic(String chroniccode)throws SQLException{
		this.chroniccode = chroniccode;
		Connection conn = null;
		try{
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			deleteFromDB(conn);
			conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	
	private void deleteFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt = null;
		try{
			StringBuffer sqlBuffer = new StringBuffer("delete from t_chronic where chroniccode=?");
			pstmt=conn.prepareStatement(sqlBuffer.toString());
			pstmt.setString(1, this.chroniccode);
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw new SQLException("删除t_chronic表时出错！");
		}finally{
			DbUtil.close(pstmt);
		}
	}

	public void editChronic(String chroniccode,String pinyincode,String chronicname)throws SQLException{
		this.chroniccode = chroniccode;
		this.pinyincode  = pinyincode;
		this.chronicname = chronicname;
		Connection conn = null;
		try{
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			editFromDB(conn);
			conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	
	private void editFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update t_chronic t set t.pinyincode=?,chronicname=? where t.chroniccode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.pinyincode);
			pstmt.setString(2, this.chronicname);
			pstmt.setString(3, this.chroniccode);
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to update t_chronic  !", e);
		} finally {
			DbUtil.close(pstmt);
		}
	}
	
	/**
	 * 
	 * @param id
	 * @throws Exception
	 */
	private void load(String chroniccode) throws UserNotFoundException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
	//	System.out.println("chroniccode:"+chroniccode);
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_chronic where chroniccode=?");
			int index = 1;
			pstmt.setString(index, chroniccode);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				this.chroniccode = rs.getString("chroniccode");
				this.pinyincode  = rs.getString("pinyincode");
				this.chronicname = rs.getString("chronicname");
			}

		} catch (SQLException e) {
			throw new UserNotFoundException("Chronic with id: " + chroniccode
					+ " could not be loaded from the database.");
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

}
